$(function () {
    $("#jqGrid").jqGrid({
        url: '../dfsp/list',
        datatype: "json",
        colModel: [			
			{ label: 'dfId', name: 'dfId', index: 'df_id', width: 50, key: true,hidden:true },
            { label: '附件id', name: 'fileId', index: 'file_id', width: 80 ,hidden:true},
            { label: '流程状态', name: 'pistatus', index: 'pistatus', width: 130 ,formatter:pistatus},
            { label: '姓名', name: 'userdesc', index: 'userdesc', width: 80 },
            { label: '结算点id', name: 'jsdId', index: 'jsd_id', width: 80 ,hidden:true},
            { label: '名称', name: 'jsdName', index: 'jsd_name', width: 80 },
            { label: '统计方式', name: 'tjfs', index: 'tjfs', width: 80 ,formatter:getDicttjfs},
            { label: '垫付类型', name: 'dfType', index: 'df_type', width: 80 ,formatter:getDictdfType},
            { label: '垫付事由', name: 'dfsy', index: 'dfsy', width: 80 },
            { label: '垫付金额', name: 'dfje', index: 'dfje', width: 80 },
            { label: '垫付开始日期', name: 'startDate', index: 'start_date', width: 80 },
            { label: '垫付结束日期', name: 'endDate', index: 'end_date', width: 80 },
            { label: '垫付月数', name: 'dfMonth', index: 'df_month', width: 80 },
            { label: '回款承诺', name: 'hkcn', index: 'hkcn', width: 80 },
			{ label: '支付日期', name: 'zfDate', index: 'zf_date', width: 80 },
            { label: '部门', name: 'dept', index: 'dept', width: 80 },
            { label: '岗位', name: 'post', index: 'post', width: 80 ,formatter:getDictInfo},
			{ label: '提交人', name: 'tjUser', index: 'tj_user', width: 80 }, 			
			{ label: '提交日期', name: 'tjDate', index: 'tj_date', width: 80 },
            { label: '审批/抄送人', name: 'nextUser', index: 'next_user', width: 90 },
            { label: '流程ID', name: 'piid', index: 'piid', width: 80,hidden:true }
		/*	{ label: '一级审核人id', name: 'yjshUserid', index: 'yjsh_userid', width: 80 },
			{ label: '一级审核人', name: 'yjshUser', index: 'yjsh_user', width: 80 }, 			
			{ label: '一级审核日期', name: 'yjshDate', index: 'yjsh_date', width: 80 }, 			
			{ label: '二级审核人id', name: 'ejshUserid', index: 'ejsh_userid', width: 80 }, 			
			{ label: '二级审核人', name: 'ejshUser', index: 'ejsh_user', width: 80 }, 			
			{ label: '二级审核日期', name: 'ejshDate', index: 'ejsh_date', width: 80 }, 			
			{ label: '三级审核人id', name: 'sjshUserid', index: 'sjsh_userid', width: 80 }, 			
			{ label: '三级审核人', name: 'sjshUser', index: 'sjsh_user', width: 80 }, 			
			{ label: '三级审核日期', name: 'sjshDate', index: 'sjsh_date', width: 80 }, 			
			{ label: '抄送人id', name: 'csId', index: 'cs_id', width: 80 }, 			
			{ label: '抄送人', name: 'csUser', index: 'cs_user', width: 80 }		*/
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    vm.getjqGridselectsalary();
    vm.getjqGridselectsetlement();
    vm.getjqGridselectsocialsb();
    var r = utils.getDictList("dflx,tjfs");
    vm.dfType = r.dflx;
    vm.tjfs = r.tjfs;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showtable:false,
		title: null,
		dfsp: {},
        tjfs:{},
        dfType:{},
		q:{
            tablename:null
		}
	},
	methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.showtable=false;
            vm.title = "新增";
            vm.dfsp = {
                jsdName:""
            };
            changetjfs();
        },
        update: function (event) {
            var dfId = getSelectedRow();
            if (dfId == null) {
                return;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",dfId);
            if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
            {
                alert("数据已提交，不能修改");
                return ;
            }
            vm.showList = false;
            vm.showtable=false;
            vm.title = "修改";

            vm.getInfo(dfId);
        },
        saveOrUpdate: function (event) {
            var tjfs = vm.dfsp.tjfs;
            var dfType = vm.dfsp.dfType;
            if (tjfs == "" || tjfs == null || tjfs == undefined || dfType == "" || dfType == null || dfType == undefined) {
                alert("请选择正确的统计方式和垫付类型！");
                return;
            }
            //console.log(vm.dfsp);

            var url = vm.dfsp.dfId == null ? "../dfsp/save" : "../dfsp/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.dfsp),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var dfIds = getSelectedRows();
            if (dfIds == null) {
                return;
            }
            var len=dfIds.length;
            for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",dfIds[i]);
                if(rowData.piid!=''&&rowData.piid!=null&&rowData.piid!='0')
                {
                    alert("数据已提交，不能删除");
                    return ;
                }
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../dfsp/delete",
                    contentType: "application/json",
                    data: JSON.stringify(dfIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (dfId) {
            $.get("../dfsp/info/" + dfId, function (r) {
                vm.dfsp = r.dfsp;
                // console.log(vm.dfsp)
                changetjfs();
            });
        },
        reload: function (event) {
            vm.showList = true;
            vm.showtable=false;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        selectunit: function () {
            var tjfs = vm.dfsp.tjfs;
            var dfType = vm.dfsp.dfType;
            // console.log(tjfs);
            // console.log(dfType);
            if (vm.dfsp.tjfs == "tjfs01") {
                this.reloadsetlement();
            } else if (vm.dfsp.tjfs == 'tjfs02' && vm.dfsp.dfType == 'dflx01') {//工资
               // vm.q.tablename = "v_salary_basicinfo";
                this.reloadsalary();
            } else if (vm.dfsp.tjfs == 'tjfs02' && vm.dfsp.dfType == 'dflx02') {
              //  vm.q.tablename = "v_socialsb_summary";
                this.reloadsocialsb();
            } else {
                alert("请选择正确的统计方式和垫付类型！");
                return;
            }
        },
        reloadsetlement: function (event) {
            var page = $("#jqGridselectsetlement").jqGrid('getGridParam', 'page');
            $("#jqGridselectsetlement").jqGrid('setGridParam', {
                datatype: 'json',
                page: page
            }).trigger("reloadGrid");
            utils.openDialogView('选择结算点', $("#windowsetlement"), '900px', '500px', 1);
        },
        getjqGridselectsetlement: function () {
            $("#jqGridselectsetlement").jqGrid({
                url: '../customsettlement/list',
                datatype: "local",
                colModel: [
                    {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
                    {label: '单位名称', name: 'unitName', index: 'unit_name'},
                    {label: '合同编号', name: 'contractNo', index: 'contract_no'},
                    {label: '结算点编码', name: 'settlementNo', index: 'settlement_no'},
                    {label: '结算点简码', name: 'settlementCode', index: 'settlement_code'},
                    {label: '结算点名称', name: 'settlementName', index: 'settlement_name'}

                ],
                viewrecords: true,
                height: 300,
                width: 900,
                rowNum: 30,
                rowList: [10, 30, 50],
                rownumbers: true,
                autowidth: false,
                //字段太多自动加载横轴滚动条
                shrinkToFit: false,
                autoScroll: false,
                multiselect: false,//只允许单选
                pager: "#jqGridPagerselectsetlement",
                jsonReader: {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order"
                },
                gridComplete: function () {
                    //显示grid底部滚动条
                    //$("#jqGridselectunit").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
                }
            });
        },
        reloadsalary: function (event) {
            var page = $("#jqGridselectsalary").jqGrid('getGridParam', 'page');
            $("#jqGridselectsalary").jqGrid('setGridParam', {
                datatype: 'json',
                // postData: {tablename: vm.q.tablename},
                page: page
            }).trigger("reloadGrid");
            utils.openDialogView('选择工资', $("#windowsalary"), '900px', '500px', 1);
        },
        getjqGridselectsalary: function () {
            $("#jqGridselectsalary").jqGrid({
                url: '../dfsp/salarylist',
                datatype: "local",
                colModel: [
                    {label: '单位id', name: 'unitid', index: 'unitid', hidden: true},
                    {label: '单位名称', name: 'unitname', index: 'unitname'},
                    {label: '年', name: 'actualsettleyear', index: 'actualsettleyear'},
                    {label: '月', name: 'actualsettlemonth', index: 'actualsettlemonth'},
                    {label: '批次号', name: 'batchcode', index: 'batchcode'},
                    {label: '汇总金额', name: 'paysum', index: 'paysum'}
                ],
                viewrecords: true,
                height: 300,
                width: 900,
                rowNum: 30,
                rowList: [10, 30, 50],
                rownumbers: true,
                autowidth: false,
                //字段太多自动加载横轴滚动条
                shrinkToFit: false,
                autoScroll: false,
                multiselect: false,//只允许单选
                pager: "#jqGridPagerselectsalary",
                jsonReader: {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order"
                },
                gridComplete: function () {
                    //显示grid底部滚动条
                    //$("#jqGridselectunit").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
                }
            });
        },
        reloadsocialsb: function (event) {
            var page = $("#jqGridselectsocialsb").jqGrid('getGridParam', 'page');
            $("#jqGridselectsocialsb").jqGrid('setGridParam', {
                datatype: 'json',
                // postData: {tablename: vm.q.tablename},
                page: page
            }).trigger("reloadGrid");
            utils.openDialogView('选择社保', $("#windowsocialsb"), '900px', '500px', 1);
        },
        getjqGridselectsocialsb: function () {
            $("#jqGridselectsocialsb").jqGrid({
                url: '../dfsp/socialsblist',
                datatype: "local",
                colModel: [
                    {label: '批次号', name: 'batchcode', index: 'batchcode'},
                    {label: '保险类型', name: 'insuredtype', index: 'insuredtype',formatter:getDictInfo},
                    {label: '汇总金额', name: 'paysum', index: 'paysum'}
                ],
                viewrecords: true,
                height: 300,
                width: 900,
                rowNum: 30,
                rowList: [10, 30, 50],
                rownumbers: true,
                autowidth: false,
                //字段太多自动加载横轴滚动条
                shrinkToFit: false,
                autoScroll: false,
                multiselect: false,//只允许单选
                pager: "#jqGridPagerselectsocialsb",
                jsonReader: {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames: {
                    page: "page",
                    rows: "limit",
                    order: "order"
                },
                gridComplete: function () {
                    //显示grid底部滚动条
                    //$("#jqGridselectunit").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
                }
            });
        },
        queryfile:function(){
            var Id = getSelectedRow();
            if(Id == null){
                return ;
            }
            var rowData = $("#jqGrid").jqGrid("getRowData", Id);
            utils.openDialogView('上传附件',"../sys/upload.html?fileId="+rowData.fileId,"900px","500px",2);
        },

        //审批

        submit: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            var rowData = $("#jqGrid").jqGrid("getRowData", id);
            var piid = rowData.piid;
            if (piid != 0 && piid != null && piid != "") {
                alert("数据已提交，不能重复提交！");
                return;
            }
            var url = "../dfsp/submit";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(id),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        processInt: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.showtable = true;
            vm.showtable1 = false;
            vm.showtable2 = true;
            vm.title = "审批";

            vm.getInfo(id)
        },

        process: function (event) {
            var url = "../dfsp/process";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.dfsp),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        reject: function (event) {
            var url = "../dfsp/reject";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.dfsp),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        popHistory: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var piid=rowData.piid;
            if(piid==0||piid==null||piid==""){
                alert("没有流程！");
                return;
            }
            var rowData = $("#jqGrid").jqGrid("getRowData", id);
            var piid = rowData.piid;
            utils.popHistory(piid);
            var page = $("#popHistoryGrid").jqGrid('getGridParam', 'page');
            $("#popHistoryGrid").jqGrid('setGridParam', {
                postData: {
                    'piid': piid,
                },
                page: page
            }).trigger("reloadGrid");
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "历史记录",
                area: ['725px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popHistory"),
                /*  btn: ['确定', '取消'],
                 btn1: function (index) {
                 layer.close(index);
                 }*/
            });
        }
    }
});
function startDate(value){
	vm.dfsp.startDate=value;
	//console.log(vm.dfsp.startDate);
}
function endDate(value){
    vm.dfsp.endDate=value;
    //console.log(vm.dfsp.endDate);
}
function zfDate(value){
    vm.dfsp.zfDate=value;
    //console.log(vm.dfsp.zfDate);
}
$("#tjfs").change(function(){
    changetjfs();
});
changetjfs=function(){
    var tjfs = vm.dfsp.tjfs;
    if (tjfs == 'tjfs01'||tjfs==""||tjfs==null||tjfs==undefined) {
       // $("#title").html("结算点名称");
        $("#changedate").attr("hidden", false);
        $("#huikuan").attr("hidden", true);
        $("#dfje").attr("readonly", false);
        vm.dfsp.jsdId=null;
        vm.dfsp.jsdName=null;
        vm.dfsp.dfje=null;
        // $("#huikuanval").text("");

    } else if (tjfs == 'tjfs02') {
       // $("#title").html("垫付单位");
        $("#changedate").attr("hidden", true);
        // $("#startDate").val("");
        // $("#endDate").val("");
        $("#huikuan").attr("hidden", false);
        $("#dfje").attr("readonly", true);

        vm.dfsp.jsdId=null;
        vm.dfsp.jsdName=null;
        vm.dfsp.dfje=null;
    }
}
$("#closeselectsetlement").click(function(){
    utils.closeCurrentPage();
});

$("#addselectsetlement").click(function(){
    //判断是否选中了数据
    var rowKey = $("#jqGridselectsetlement").getGridParam("selrow");
    if(!rowKey){
        alert("请选择一条记录");
        return ;
    }
    var rowData = $("#jqGridselectsetlement").jqGrid("getRowData", rowKey)
    vm.dfsp.jsdId=rowData.id;
    vm.dfsp.jsdName=rowData.unitName;
    //vm.dfsp.dfje=rowData.paysum;
    utils.closeCurrentPage();
});

$("#closeselectsalary").click(function(){
    utils.closeCurrentPage();
});

$("#addselectsalary").click(function(){
    //判断是否选中了数据
    var rowKey = $("#jqGridselectsalary").getGridParam("selrow");
    if(!rowKey){
        alert("请选择一条记录");
        return ;
    }
    var rowData = $("#jqGridselectsalary").jqGrid("getRowData", rowKey)
    vm.dfsp.jsdName=rowData.unitname+rowData.actualsettleyear+rowData.actualsettlemonth+rowData.batchcode;
    vm.dfsp.dfje=rowData.paysum;
    utils.closeCurrentPage();
});

$("#closeselectsocialsb").click(function(){
    utils.closeCurrentPage();
});

$("#addselectsocialsb").click(function(){
    //判断是否选中了数据
    var rowKey = $("#jqGridselectsocialsb").getGridParam("selrow");
    if(!rowKey){
        alert("请选择一条记录");
        return ;
    }
    var rowData = $("#jqGridselectsocialsb").jqGrid("getRowData", rowKey)
    vm.dfsp.jsdName=rowData.insuredtype+rowData.batchcode;
    vm.dfsp.dfje=rowData.paysum;
    utils.closeCurrentPage();
});



function getDicttjfs(cellvalue, options, rowObject){
    var html="";
    for(var i=0; i<vm.tjfs.length; i++){
        if(vm.tjfs[i].value==cellvalue){
            html=vm.tjfs[i].label;
        }
        if(html==undefined){
            html="";
        }
    }
    return html;
}
function getDictdfType(cellvalue, options, rowObject){
    var html="";
    for(var i=0; i<vm.dfType.length; i++){
        if(vm.dfType[i].value==cellvalue){
            html=vm.dfType[i].label;
        }
        if(html==undefined){
            html="";
        }
    }
    return html;
}